<?php

class Roles_AjaxCrudController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout()->disableLayout();
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $this->_user_info = Zend_Auth::getInstance()->getStorage()->read();
            $this->_acl = new Zwei_Admin_Acl($this->_user_info->user_name);
            $this->_page = "roles";
        } else {
            $this->_redirect('index/login');//[TODO] se espera un JSON hacer una salida ad-hok
        }
    }

    public function indexAction()
    {
        exit("bye :)");
    }

    public function actionModuleRoleAction()
    {
        $this->view->status = "false";
        $this->view->message = "";
        $this->view->base_url = str_replace("/", "\\/", BASE_URL);
        $form = new Zwei_Utils_Form();//$_REQUEST
        Debug::write($form);
        
        if (!empty($form->action) && !empty($form->modules_id) && !empty($form->roles_id) && !empty($form->permission)) {
            $aclPermissions = new AclPermissionsModel();
            switch ($form->action) {
                //CREATE
                case 'C':
                    $data = array("acl_modules_id" => $form->modules_id,
                                  "acl_roles_id" => $form->roles_id,
                                  "permission" => $form->permission);
                    try {
                        $response = $aclPermissions->insert($data);
                    } catch (Zend_Db_Exception $e) {
                        Debug::write($data);
                        $this->view->status = $e->getCode();
                        $this->view->message = $e->getMessage();
                    }    
                    break;
                //DELETE
                case 'D':
                    $where = array("acl_modules_id" => $form->modules_id,
                                   "acl_roles_id" => $form->roles_id,
                                   "permission" => $form->permission);
                    try {
                        $response = $aclPermissions->delete($where);
                        Debug::write($where);
                    } catch (Zend_Db_Exception $e) {
                        $this->view->status = $e->getCode();
                        $this->view->message = $e->getMessage();
                    }                
            }
            
            if ($response) {
                $this->view->status = "true";
                $this->view->message = "ok";
            } 
        }
    }

    public function roleAction()
    {
        // action body
    }


}





